package com.example.hyw.szysteam.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.hyw.szysteam.POJO.ClassItem;
import com.example.hyw.szysteam.R;

import java.util.ArrayList;

/**
 * Created by HYW on 2018/6/7.
 */

public class ClassAdapter extends ArrayAdapter<ClassItem>{
    private int resource;
    private ArrayList<ClassItem> classes;

    public ClassAdapter(@NonNull Context context, int resource, @NonNull ArrayList<ClassItem> classesArrayList) {
        super(context, resource,classesArrayList);
        this.resource=resource;
        classes=classesArrayList;
    }

    @Nullable
    @Override
    public ClassItem getItem(int position) {
        return classes.get(position);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ClassItem classes=getItem(position);
        View view;
        ViewHolder viewHolder;
        if(convertView==null){
            view= LayoutInflater.from(getContext()).inflate(resource,null);
            viewHolder=new ViewHolder();
            viewHolder.className=view.findViewById(R.id.tv_classname);
            viewHolder.role=view.findViewById(R.id.tv_role);
            viewHolder.teacher=view.findViewById(R.id.tv_teacher);
            viewHolder.isTeacher=view.findViewById(R.id.isteacher);
            view.setTag(viewHolder);
        }else{
            view=convertView;
            viewHolder=(ViewHolder)view.getTag();
        }
        viewHolder.className.setText(classes.getClassname());
        if(classes.getRole()){
            viewHolder.role.setText("老师");
            viewHolder.isTeacher.setVisibility(View.GONE);

        }else {
            viewHolder.role.setText("学生");
            viewHolder.teacher.setText(classes.getTeacher());
            viewHolder.isTeacher.setVisibility(View.VISIBLE);
        }
        return view;
    }

    class ViewHolder{
        TextView className;
        TextView role;
        TextView teacher;
        TextView isTeacher;
    }
}
